package org.linlinjava.litemall.admin.controller;

import java.util.HashMap;
import java.util.Map;

import org.linlinjava.litemall.admin.beans.annotation.LogAnno;
import org.linlinjava.litemall.admin.beans.annotation.LoginAdminShopId;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.db.service.LitemallGoodsService;
import org.linlinjava.litemall.db.service.LitemallMerchandiseService;
import org.linlinjava.litemall.db.service.LitemallOrderService;
import org.linlinjava.litemall.db.service.LitemallShopMerchandiseService;
import org.linlinjava.litemall.db.service.LitemallUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 管理dashbord控制器
 *
 * @author yliyun
 * @date 2022/09/14
 */
@RestController
@RequestMapping("/admin/dashboard")
@Validated
public class AdminDashbordController {

    /**
     * 用户服务
     */
    @Autowired
    private LitemallUserService userService;
    /**
     * 商品服务
     */
    @Autowired
    private LitemallGoodsService goodsService;
    /**
     * 订单服务
     */
    @Autowired
    private LitemallOrderService orderService;
    /**
     * 商品服务
     */
    @Autowired
    private LitemallMerchandiseService merchandiseService;
    /**
     * 商店商品服务
     */
    @Autowired
    private LitemallShopMerchandiseService shopMerchandiseService;


    /**
     * 信息
     *
     * @param shopId 商店id
     * @return {@link Object}
     */
    @GetMapping("")
    @LogAnno
    public Object info(@LoginAdminShopId Integer shopId) {
        int userTotal = userService.countNoInner();
        Map<String, Integer> data = new HashMap<>();
        data.put("userTotal", userTotal);
        data.put("goodsTotal", shopId == null ? goodsService.count() :goodsService.count(shopId));
        data.put("productTotal", shopId == null ? (int)merchandiseService.count() : shopMerchandiseService.count(shopId));
        data.put("orderTotal", shopId == null ? orderService.count2() : orderService.countShop(shopId));

        return ResponseUtil.ok(data);
    }
}
